package pe.edu.upc.qwerty.movil;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import pe.edu.upc.dsd.qwerty.wsreserva.model.Asiento;
import pe.edu.upc.dsd.qwerty.wsreserva.model.Asientos;
import pe.edu.upc.qwerty.movil.service.RestServices;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class FrmAsientos  extends Activity  {
	
	private static final String TAG = "ASTS";	
	private RestServices restServices;
	private String[] asientos;
	private RadioGroup group;
	private String idVuelo;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frmasientos);
        group=(RadioGroup)findViewById(R.id.grupoUno);
        Log.d(TAG, group + " aki el group");
        
        Button reservar = (Button)findViewById(R.id.btnReservar);
        //reservar.
        //reservar.
        
        restServices = new RestServices();
        
        Log.d(TAG,"cargo pantalla asientos");
        Bundle bundle = getIntent().getExtras();
        try {        	
        	idVuelo = bundle.getString("codVuelo");
        	cargaSimple(idVuelo );	
		} catch (Exception e) {
			e.printStackTrace();
			Log.d(TAG,e.getMessage());			
		}
		
		/*ListView lv = getListView();
	    lv.setTextFilterEnabled(true);
        
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener  () {
	          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		           Log.d("LISTA", "Seleccionado = " + position);
		           TextView txt =(TextView)parent.getChildAt( position ).findViewById( R.id.list );
	               String keyword = txt.getText().toString();
	               Log.d(TAG, keyword);
	          }
        });*/
	}
	
	public void cargaSimple(String idVuelo){		
		asientos =listaAsientos(idVuelo);//new String[] {"AA-522","TA-666","LF-963"};	//
		String[] oba = new String[asientos.length];
		int i = 0;
		for (String asiento : asientos) {
			oba[i] = asiento.split(",")[1];
			i++;
		}
		
		
		//setListAdapter(new ArrayAdapter<String>(this, R.layout.lista, oba ));
		i = 0;
		for (String string : oba) {
			RadioButton button=new RadioButton(FrmAsientos.this);
			String asiento = asientos[i].split(",")[0];
			button.setId(Integer.parseInt(asiento));
			button.setText(string);
		    group.addView(button);
		    i++;
		}
		
	}
	
	public String[] listaAsientos(String idVuelo){
    	String[] res= null;
    	try {	
	   		 Log.d(TAG, "id vuelo "+ idVuelo + "" );
	   		 String url = "http://64.76.72.75/wsreserva/services/rest/vuelos/"+idVuelo+"/asientos";
	   		Asientos result = restServices.getAsientos(url);
	   		 Log.d(TAG, "LISTA resultado = " + result.getAsientos().size());
	   		 res = new String[result.getAsientos().size()];
	   		 int i = 0 ;
	   		 
	   		for(Asiento as : result.getAsientos()){
	   			res[i++] = as.getIdAsiento()+","+as.getDescripcion() + " - " + as.getNumero();
	   		}
    		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
    }
	
	public void reservaHandler(View view) {
		int id = group.getCheckedRadioButtonId();
		/*String url = "http://64.76.72.75/wsreserva/services/rest/vuelos/"+idVuelo+"/asientos/"+id+"/";
   		Asientos result = restServices.getAsientos(url);*/
		
		SoapObject request = new SoapObject(Lista.NAMESPACE, "reservarAsiento");
    	request.addProperty("usuario", VuelosActivity.USUARIO_LOGIN);
    	request.addProperty("password", VuelosActivity.PASS_LOGIN);
    	request.addProperty("idAsiento", id);
    	
    	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(Lista.URL,Lista.TIMEOUT);
        SoapObject respuesta = null;
     	try {
			androidHttpTransport.call( Lista.NAMESPACE+"reservarAsiento" , envelope);
			respuesta = (SoapObject)envelope.getResponse();
			//respuesta.get
			
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Reserva Realizada");
		alertDialog.setMessage("Se le enviara un correo para validar su reserva. Gracias");
		alertDialog.setButton("Cerrar", new DialogInterface.OnClickListener() {
		   public void onClick(DialogInterface dialog, int which) {
		      
		   }
		});
		alertDialog.setIcon(R.drawable.icon);
		alertDialog.show();
	}
    
	
}
